<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="(url, i) in bannerList" :key="i">
        <img :src="url" />
      </div>
    </div>
    <!-- 如果需要分页器 -->
    <div class="swiper-pagination"></div>

    <!-- 如果需要导航按钮 -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
  </div>
</template>

<script>
//第一步:引入依赖包、样式
import Swiper from 'swiper'
import 'swiper/css/swiper.min.css'
export default {
  props: {
    bannerList: {
      require: true,
      default: []
    }
  },
  data() {
    return {
      swiper: null
    }
  },
  watch: {
    bannerList: {
      immediate: true,
      handler: function (newVal) {
        if (!newVal || newVal.length === 0) return
        //1:swiper插件,对外暴露一个Swiper构造函数
        //2:Swiper构造函数需要传递参数 1、结构总根节点CSS选择器|根节点真实DOM节点  2、轮播图配置项
        //3.new Swiper 时,必须保证dom节点已经渲染好了:
        //3.1 watch监听bannerList 保证数据已经获取
        //3.2 nextTick 保证dom 节点 v-for 渲染完毕
        this.$nextTick(() => {
          this.swiper = new Swiper('.swiper-container', {
            //设置轮播图水平
            direction: 'horizontal',
            //开启循环模式
            loop: true,
            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination',
              //分页器类型
              type: 'bullets',
              //点击分页器，切换轮播
              clickable: true
            },
            //自动轮播
            autoplay: {
              delay: 1000,
              //新版本的写法
              // pauseOnMouseEnter: true,
              //如果设置为true，当切换到最后一个slide时停止自动切换
              stopOnLastSlide: true,
              //用户操作swiper之后，是否禁止autoplay
              disableOnInteraction: false
            },
            // 如果需要前进后退按钮
            navigation: {
              nextEl: '.swiper-button-next',
              prevEl: '.swiper-button-prev'
            }
            //切换效果
            // effect: "cube",
          })
        })
      }
    }
  }
}
</script>

<style></style>
